﻿using Blm.Utils.Extensions;
using Language.Resource;
using Microsoft.Extensions.Localization;
using System;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Sys;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Sys;
using VisionCloud.Domain.Repository.Interface.Ms;
using VisionCloud.Domain.Repository.Interface.Sys;
using VisionCloud.Domain.UnitOfWork;

namespace VisionCloud.Service.Sys
{
    public class UserProjectService
    {
        public IUnitOfWork UnitOfWork { get; set; }

        public ISysUserProjectRepository SysUserProjectRepository { get; set; }

        public ISysRoleRepository SysRoleRepository { get; set; }

        public IMsProjectRepository MsProjectRepository { get; set; }

        public IMsTaskRepository MsTaskRepository { get; set; }

        /// <summary>
        /// 多语言资源类
        /// </summary>
        public IStringLocalizer<ServiceResource> ServiceLocalizer { get; set; }

        #region 用户项目

        public async Task<ApiPageResult> GetPage(SysUserProjectCondition conditon)
        {
            return await SysUserProjectRepository.GetPage(conditon);
        }

        public async Task<bool> IsAdmin(SysUserRoleDto model)
        {
            return await SysRoleRepository.AnyAsync(p => p.RoleId == model.RoleId && p.IsSuperAdministrator == true && p.IsDeleted == false);
        }

        /// <summary>
        /// 数据是否存在
        /// </summary>
        /// <param name="model"></param>
        /// <param name="isDeleted">是否检索标志为删除的</param>
        /// <returns></returns>
        public async Task<bool> Existed(SysUserProjectDto model, bool isDeleted = false)
        {
            return await SysUserProjectRepository.AnyAsync(p => p.ProjectId == model.ProjectId && p.UserId == model.UserId);
        }

        public async Task CreateUserProject(SysUserProjectDto model, Guid opeId)
        {
            var exists = await Existed(model);
            if (exists == true)
            {
                throw new OperationException(ServiceLocalizer["YB0331"].Value);
            }

            var entity = model.CopyTo<SysUserProject>();
            entity.UserProjectId = Guid.NewGuid();
            SysUserProjectRepository.Add(entity);
            await UnitOfWork.SaveChangesAsync();
            //清除项目成员缓存
            MsTaskRepository.RemoveUserProjects();
        }

        public async Task DeleteUserProject(SysUserProjectDto model, Guid opeId)
        {
            var exists = await Existed(model);
            if (!exists == true)
            {
                throw new OperationException(ServiceLocalizer["YB0332"].Value);
            }

            //不能删除项目的创建者
            var prjCreatedUser = await MsProjectRepository.WhereAsync(p => p.CreatedBy == model.UserId && p.ProjectId == model.ProjectId);
            if (prjCreatedUser.Count > 0)
            {
                throw new OperationException(ServiceLocalizer["YB0333"].Value);
            }

            var items = await SysUserProjectRepository.WhereAsync(t =>
                (t.UserId == model.UserId && t.ProjectId == model.ProjectId));

            await UnitOfWork.OpenTrans(async (save) =>
           {
               foreach (var item in items)
               {
                   SysUserProjectRepository.Remove(item, true);
               }
           });
            //清除项目成员缓存
            MsTaskRepository.RemoveUserProjects();
        }

        public async Task UpdateUserProject(SysUserProjectDto model, Guid opeId)
        {
            var exists = await Existed(model);
            if (!exists == true)
            {
                throw new OperationException(ServiceLocalizer["YB0332"].Value);
            }

            SysUserProjectRepository.Update(t => (t.UserId == model.UserId && t.ProjectId == model.ProjectId
                                                                           //&& t.IsDeleted == false
                                                                           ), d =>
            {
                d.ProjectId = model.ProjectId;
                //d.LastUpdatedBy = opeId;
                //d.LastUpdatedDate = DateTime.Now;
            });
            await UnitOfWork.SaveChangesAsync();
            //清除项目成员缓存
            MsTaskRepository.RemoveUserProjects();
        }

        #endregion 用户项目
    }
}